/*
 * Copyright (C) 2010-2011 Comcast Cable.  All Rights Reserved.
 */
package com.hercules.android.receiver.errorhandlers;

import com.hercules.android.receiver.AndroidReceiver;
import com.hercules.android.receiver.AndroidReceiverErrorHandler;
import com.hercules.android.receiver.ApplicationConnectData;

/**
 * Default implementation of {@link AndroidReceiverErrorHandler}, it prints all errors to System.err.
 * <p>
 * <p>
 * AndroidAssembly1 this class was added in Android Assembly 1
 * <p>
 * <b>Thread safety:</b> It is immutable and thread safe
 *
 * @author 7realm
 * @version 1.0
 */
public class DefaultAndroidReceiverErrorHandler implements AndroidReceiverErrorHandler {
    /**
     * Handles the closing error in the given receiver. The error is described in the message.
     *
     * @param androidReceiver the AndroidReceiver raising the error
     * @param message the error message
     */
    public void onCloseError(AndroidReceiver androidReceiver, String message) {
        describeReceiver(androidReceiver);
        System.err.println("Close error: " + message);
    }

    /**
     * Handles the connection error in the given receiver. The error is described in the message.
     *
     * @param androidReceiver the AndroidReceiver raising the error
     * @param message the error message
     */
    public void onConnectError(AndroidReceiver androidReceiver, String message) {
        describeReceiver(androidReceiver);
        System.err.println("Connect error: " + message);
    }

    /**
     * Handles the shutdown error in the given receiver. The error is described in the message.
     *
     * @param androidReceiver the AndroidReceiver raising the error
     * @param message the error message
     */
    public void onShutdownError(AndroidReceiver androidReceiver, String message) {
        describeReceiver(androidReceiver);
        System.err.println("Shutdown error: " + message);
    }

    /**
     * Prints some information about receiver.
     *
     * @param receiver the current android receiver
     */
    private static void describeReceiver(AndroidReceiver receiver) {
        ApplicationConnectData conData = receiver.getApplicationConnectData();
        System.err.println("Error occurred in android receiver, url "
            + (conData == null ? "not set" : conData.getUrl()));
    }
}
